﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;


namespace HX.HxDemo.BOS.Business.PlugIn01
{
    [Description("【表单插件】将日志写入日志库"),HotUpdate]
    public class WriteToLogDbFormPlugIn : AbstractDynamicFormPlugIn
    {
        public override void AfterDoOperation(AfterDoOperationEventArgs e)
        {
            base.AfterDoOperation(e);
            var log = string.Empty;
            if(e.OperationResult != null)
            {
                log = e.OperationResult.IsSuccess ? "成功" : "失败";
                if(e.OperationResult.OperateResult != null && e.OperationResult.OperateResult.Count > 0)
                {
                    log += "\r\n操作结果：" + string.Join("\r\n", e.OperationResult.OperateResult.Select(o => o.Message));
                }

                if(e.OperationResult.ValidationErrors != null && e.OperationResult.ValidationErrors.Count > 0)
                {
                    log += "\r\n校验错误：" + string.Join("\r\n", e.OperationResult.ValidationErrors.Select(o => o.Message));
                }
            }
            DbLogger.AddOperateLog(this.Context, this.View.BillBusinessInfo, e.Operation.OperationName, log);
        }
    }

    public class DbLogger
    {
        public static bool AddOperateLog(Context ctx,BusinessInfo businessInfo,string operationName,string log)
        {
            var sql = @"INSERT INTO Hx_Log_OperateLog(FID,F_MDCE_OBJECTTYPEID,F_MDCE_OBJECTTYPENAME,F_MDCE_OPERATIONNAME,F_MDCE_CREATORID,F_MDCE_CREATORNAME,F_MDCE_CREATETIME,F_MDCE_LOG)
                        VALUES(@FID,@F_MDCE_OBJECTTYPEID,@F_MDCE_OBJECTTYPENAME,@F_MDCE_OPERATIONNAME,@F_MDCE_CREATORID,@F_MDCE_CREATORNAME,@F_MDCE_CREATETIME,@F_MDCE_LOG)";
            var sqlParameterList = new List<SqlParam>();
            sqlParameterList.Add(new SqlParam("@FID", KDDbType.AnsiString, SequentialGuid.NewGuid().ToString().Replace("_", "")));
            sqlParameterList.Add(new SqlParam("@F_MDCE_OBJECTTYPEID", KDDbType.AnsiString, businessInfo.GetForm().Id));
            sqlParameterList.Add(new SqlParam("@F_MDCE_OBJECTTYPENAME", KDDbType.AnsiString, businessInfo.GetForm().Name.ToString()));
            sqlParameterList.Add(new SqlParam("@F_MDCE_OPERATIONNAME", KDDbType.AnsiString, operationName));
            sqlParameterList.Add(new SqlParam("@F_MDCE_CREATORID", KDDbType.Int64, ctx.UserId));
            sqlParameterList.Add(new SqlParam("@F_MDCE_CREATORNAME", KDDbType.AnsiString, ctx.UserName));
            sqlParameterList.Add(new SqlParam("@F_MDCE_CREATETIME", KDDbType.DateTime, DateTime.Now));
            sqlParameterList.Add(new SqlParam("@F_MDCE_LOG", KDDbType.AnsiString, log));
            var returnValue = DBUtils.Execute(ctx.LogDBContext, sql, sqlParameterList);
            return returnValue > 0;
        }
    }
}
